package com.jxj.controller;

import com.jxj.dao.SocRecruitMsgMapper;
import com.jxj.entity.Resp;
import com.jxj.entity.SocRecruitMsg;
import com.jxj.entity.SocRecruitMsgVO;
import com.jxj.util.GetSocMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@RestController
@Slf4j
public class SocRecruitMsgController {
    @Resource
    private SocRecruitMsgMapper socRecruitMsgMapper;
    static String ip = "https://hd.jxj777.top";
    @PostMapping("/sendstinf")
    public Resp socRecruitPost(SocRecruitMsg socRecruitMsg,
                               MultipartFile video,
                               MultipartFile index_img
//                               MultipartFile imgs[]
                               ) throws IOException {
        if (video != null) {
            String originalFilename = video.getOriginalFilename();
            String classespath = "/usr/img/" + originalFilename;
            video.transferTo(new File(classespath));
            String filePath = ip + "/usr/img/" + originalFilename;
            socRecruitMsg.setVideo_path(filePath);
            log.info(filePath);
        } else {
            socRecruitMsg.setVideo_path("");
        }
        if (index_img != null) {
            String originalFilename = index_img.getOriginalFilename();
            String classespath = "/usr/img/" + originalFilename;
            index_img.transferTo(new File(classespath));
            String filePath = ip + "/usr/img/" + originalFilename;
            socRecruitMsg.setIndex_img_path(filePath);
            log.info(filePath);
        } else {
            socRecruitMsg.setIndex_img_path("");
        }
//        if(imgs.length > 0){
//            for (MultipartFile img : imgs) {
//                String originalFilename = index_img.getOriginalFilename();
//                String classespath = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "static/upload/" + originalFilename;
//                index_img.transferTo(new File(classespath));
//                String filePath = ip + "/upload/" + originalFilename;
//                socRecruitMsg.setIndex_img_path(filePath);
//                log.info(filePath);
//            }
//        }
    int flag = socRecruitMsgMapper.addSocRecruit(socRecruitMsg);
        if(flag == 1)
        {
            return new Resp(HttpStatus.OK,null);
        }
        else
            {
                return new Resp(HttpStatus.BAD_REQUEST,null);
            }
    }

    /**
     * @param id
     * @return
     */
    @GetMapping("/getstinfor")
    public Resp<SocRecruitMsgVO> selectSocRecruit(int id)
    {
        Map<Integer, String> map = GetSocMapping.getMap();
        SocRecruitMsgVO socRecruitMsg = socRecruitMsgMapper.selectSocRecruit(map.get(id));
        return new Resp<>(HttpStatus.OK, "查询成功", socRecruitMsg);
    }
}
